using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using cframework_xlua;
using System.ComponentModel;
using Newtonsoft.Json;
namespace cframework_xlua
{
    [CreateAssetMenu(menuName = "cframework_xlua/PublishUtil_Wx")]
    public class PublishUtil_Wx : PlatformPublishUtil
    {
        public override bool BuildAndPublish(BuildPlayerSetting setting, BuildPlayerSetting.PublishInfo publishInfo)
        {
            var ossDir = $"{publishInfo.appName}/{publishInfo.middleDir}/{publishInfo.hotupdateVersionNumber}_{publishInfo.timestamp}";
            var note = $"{publishInfo.realAppVersion}_{publishInfo.hotupdateVersionNumber}_{publishInfo.branchName}_{publishInfo.lastChangedVersion}";
            if (!WXTool.DoExport(publishInfo.outputProjectPath, setting.fileSystemUtil.GetDownloadUrl() + $"/{ossDir}"))
            {
                EditorUtility.DisplayDialog("export wx minigame failed", "export wx minigame failed", "ok");
                return false;
            }

            if (!setting.fileSystemUtil.Upload(publishInfo.outputProjectPath + "/webgl", ossDir))
            {
                EditorUtility.DisplayDialog("fileSystemUtil.Upload", "exportfileSystemUtil.Upload", $"{publishInfo.outputProjectPath + "/webgl"} => {ossDir}");
                return false;
            }

            string downloadUrl = null;
            if (setting.channelUtil)
            {
                if (!setting.channelUtil.Deploy(setting, publishInfo.outputProjectPath + "/minigame", null, $"-v {setting.appVersion} -d {note}", ref downloadUrl))
                {
                    EditorUtility.DisplayDialog("deploy failed", "deploy failed", $"{publishInfo.outputProjectPath + "/webgl"} => {ossDir}");
                    return false;
                }

                setting.StartHttpServer();

                Debug.Log("download url: " + downloadUrl);
                if (EditorUtility.DisplayDialog("download url", downloadUrl, "copy", "ok"))
                {
                    GUIUtility.systemCopyBuffer = downloadUrl;
                }
            }

            return true;
        }


    }
}